#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<algorithm>

using namespace std;

namespace zy
{
	template<class T>
	struct ListNode
	{
		ListNode<T>* _prev;
		ListNode<T>* _next;
		T _data;

		ListNode(const T& val = T())
			:_prev(nullptr)
			, _next(nullptr)
			, _data(val)
		{}

		ListNode(T&& val)
			:_prev(nullptr)
			, _next(nullptr)
			, _data(move(val))
		{}
	};




	template<class T, class Ref, class Ptr>
	struct listIterator
	{
		typedef ListNode<T> Node;
		typedef listIterator<T, Ref, Ptr> Self;

		Node* _node;

		listIterator(Node* node)
			:_node(node)
		{}

		bool operator!=(const Self& x)
		{
			return _node != x._node;
		}

		// ++it
		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		// it++
		Self operator++(int)
		{
			Self tmp(*this);
			_node = _node->_next;
			return tmp;
		}
		// --it
		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		// it--
		Self operator--(int)
		{
			Self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}

		// *it
		Ref operator*()
		{
			return _node->_data;
		}

		// it->a1
		Ptr operator->()
		{
			return &(_node->_data);
		}
	};


	template<class T>
	class list
	{
	public:
		typedef ListNode<T> Node;
		typedef listIterator<T, T&, T*> iterator;
		typedef listIterator<T, const T&, const T*> const_iterator;

		void InitEmpty()
		{
			_head = new Node;
			_head->_prev = _head;
			_head->_next = _head;
		}

		list()
		{
			InitEmpty();
		}
		list(const list<T>& x)
		{
			InitEmpty();
			for (auto& e : x)
			{
				push_back(e);
			}
		}

		void swap(list<T> tmp)
		{
			std::swap(_head, tmp._head);
		}

		list<T>& operator=(list<T> tmp)
		{
			swap(tmp);
			return *this;
		}



		/*void push_back(const T& val)
		{
			Node* newnode = new Node(val);
			Node* tail = _head->_prev;

			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;
		}*/

		template<class T>
		void push_back(T&& val)
		{
			insert(end(), std::forward<T>(val));
		}
		//void push_back(const T& val)
		//{
		//	insert(end(), val);
		//}
		//void push_back(T&& val)
		//{
		//	insert(end(), move(val));
		//}

		void push_front(const T& val)
		{
			insert(begin(), val);
		}
		void pop_back()
		{
			erase(--end());
		}
		void pop_front()
		{
			erase(begin());
		}

		iterator begin()
		{
			return _head->_next;
		}
		iterator end()
		{
			return _head;
		}
		const_iterator begin() const
		{
			return _head->_next;
		}
		const_iterator end() const
		{
			return _head;
		}

		template<class T>
		void insert(iterator pos, T&& val)
		{
			Node* newnode = new Node(std::forward<T>(val));
			Node* cur = pos._node;
			Node* p = cur->_prev;

			p->_next = newnode;
			newnode->_prev = p;
			newnode->_next = cur;
			cur->_prev = newnode;
		}
		/*void insert(iterator pos, const T& val)
		{
			Node* newnode = new Node(val);
			Node* cur = pos._node;
			Node* p = cur->_prev;

			p->_next = newnode;
			newnode->_prev = p;
			newnode->_next = cur;
			cur->_prev = newnode;
		}

		void insert(iterator pos, T&& val)
		{
			Node* newnode = new Node(move(val));
			Node* cur = pos._node;
			Node* p = cur->_prev;

			p->_next = newnode;
			newnode->_prev = p;
			newnode->_next = cur;
			cur->_prev = newnode;
		}*/

		iterator erase(iterator pos)
		{
			Node* cur = pos._node;
			Node* p = cur->_prev;
			Node* n = cur->_next;

			p->_next = n;
			n->_prev = p;
			delete cur;
			return n;
		}

		bool empty()
		{
			return size() == 0;
		}

		size_t size()
		{
			int i = 0;
			for (auto& e : *this)
			{
				i++;
			}
			return i;
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}



	private:
		Node* _head;
	};

	void PrintList(list<int>& x)
	{
		list<int>::iterator it = x.begin();
		while (it != x.end())
		{
			//cout << (*it)._a1 << " " << (*it)._a2 << endl;
			cout << *it << " ";
			//*it += 2;
			//cout << *it << " ";
			it++;
		}
		cout << endl;
	}
}



